import { IBagTabElement } from "../configs/BagTab";
import { GameConfig } from "../configs/GameConfig";
import { IItemElement } from "../configs/Item";
import { UIPool } from "../etc/UIPool";
import { util } from "../etc/Util";
import { ItemModuleC, ItemModuleData, ItemValueObject } from "../module/ItemModule";
import BagItem_Generate from "../ui-generate/BagItem_generate";
import BagTab_Generate from "../ui-generate/BagTab_generate";
import BagWindow_Generate from "../ui-generate/BagWindow_generate";

export class BagWindow extends BagWindow_Generate {
    private selectedTab: BagTab
    private selectedItem: BagItem
    private itemData: ItemModuleData
    private itemMD: ItemModuleC

    private tabPool: UIPool<BagTab> = new UIPool(() => {
        let item = mw.UIService.create(BagTab)
        this.con_tabs.addChild(item.uiObject)
        return item
    })

    private itemsPool: UIPool<BagItem> = new UIPool(() => {
        let item = mw.UIService.create(BagItem)
        this.con_items.addChild(item.uiObject)
        return item
    })

    onStart() {
        this.itemData = DataCenterC.getData(ItemModuleData)
        this.itemMD = ModuleService.getModule(ItemModuleC)

        this.btn_close.onClicked.add(() => {
            mw.UIService.hideUI(this)
        })
        this.btn_equip.onClicked.add(()=>{
            this.onClickedEquipOrOffEquip()
        })
    }

    onShow() {
        // 填充页签
        this.tabPool.resetAll()
        for (const cfg of GameConfig.BagTab.getAllElement()) {
            let tab = this.tabPool.get()
            tab.setInfo(cfg)
        }
        // 点击第一个页签
        this.onClickedTab(this.tabPool.firstActiveItem)
        // 监听物品变更
        this.itemData.onDataChange.add(this.onItemDataChanged, this)
    }

    onHide(){
        this.itemData.onDataChange.remove(this.onItemDataChanged, this)
    }

    private onItemDataChanged() {
        // 填充items
        this.refreshAllItems()
        // 刷新右侧详情面板
        this.onClickedItem(this.selectedItem)
    }

    onClickedTab(selectedTab: BagTab) {
        this.selectedTab = selectedTab
        // 设置页签选中状态
        this.tabPool.eachVisibleItem(tab => {
            tab.setSelected(selectedTab == tab)
        })
        // 填充items
        this.refreshAllItems()
        // 点击第一个物体
        if (this.itemsPool.count > 0) {
            this.onClickedItem(this.itemsPool.firstActiveItem)
            this.txt_empty.visibility = SlateVisibility.Hidden
        }else{
            this.txt_empty.visibility = SlateVisibility.Visible
        }
    }

    onClickedItem(itemUI: BagItem) {
        this.selectedItem = itemUI
        this.img_selected_icon.imageGuid = itemUI.itemCfg.Icon
        this.txt_selected_describe.text = itemUI.itemCfg.Describe ?? ''
        this.txt_selected_name.text = itemUI.itemCfg.Name
        util.setImageIconByItemCfgID(this.img_selected_icon, itemUI.itemCfg.ID)

        this.itemsPool.eachVisibleItem(bagItem => {
            bagItem.setSelected(itemUI == bagItem)
        })
        // 是否可以装备
        this.btn_equip.visibility = itemUI.itemCfg.TargetGrid != null ? SlateVisibility.Visible : SlateVisibility.Collapsed
        // 是否已装备
        this.txt_equip.text = ItemModuleData.gridInBag(itemUI.grid) ? '装备' : '卸下'
    }

    private refreshAllItems(){
        this.itemsPool.resetAll()
        this.itemData.eachBagAllItem((grid, itemVo) => {
            let itemCfg = GameConfig.Item.getElement(itemVo.baseInfo.cfgID)
            if (itemCfg.Type != this.selectedTab.tabCfg.Type) return
            let itemUI = this.itemsPool.get()
            itemUI.setInfo(grid, itemVo)
        })
    }

    private onClickedEquipOrOffEquip(){
        if(!this.selectedItem) return
        
        if(ItemModuleData.gridInBag(this.selectedItem.grid)){
            this.itemMD.request_equip_item(this.selectedItem.grid)
        }else{
            this.itemMD.request_off_equip_item(this.selectedItem.grid)
        }
    }
}

class BagTab extends BagTab_Generate {
    tabCfg: IBagTabElement
    itemMD: ItemModuleC

    onStart() {
        this.itemMD = ModuleService.getModule(ItemModuleC)
        this.btn_func.onClicked.add(() => {
            UIService.getUI(BagWindow).onClickedTab(this)
        })
    }

    setInfo(tabCfg: IBagTabElement) {
        this.tabCfg = tabCfg
        this.txt_content.text = tabCfg.Name
    }

    setSelected(select: boolean) {
        this.btn_func.normalImageColor = LinearColor.colorHexToLinearColor(select ? '#FFFF10' : 'FFFFFF')
    }
}

class BagItem extends BagItem_Generate {
    itemCfg: IItemElement
    itemMD: ItemModuleC
    itemData: ItemModuleData

    grid: number
    itemVo: ItemValueObject

    onStart() {
        this.itemData = DataCenterC.getData(ItemModuleData)
        this.itemMD = ModuleService.getModule(ItemModuleC)
        this.btn_func.touchMethod = ButtonTouchMethod.PreciseTap
        this.btn_func.onClicked.add(() => {
            UIService.getUI(BagWindow).onClickedItem(this)
        })
    }

    setInfo(grid: number, itemVo: ItemValueObject) {
        this.grid = grid
        this.itemVo = itemVo
        this.itemCfg = GameConfig.Item.getElement(itemVo.baseInfo.cfgID)
        this.refresh()
    }

    refresh(){
        this.txt_name.text = this.itemCfg.Name
        util.setImageIconByItemCfgID(this.img_icon, this.itemCfg.ID)
        let equipped = ItemModuleData.gridInBag(this.grid) == false
        if (equipped) {
            this.img_equipped.visibility = SlateVisibility.SelfHitTestInvisible
        } else {
            this.img_equipped.visibility = SlateVisibility.Collapsed
        }
        let qualityCfg = GameConfig.ItemQuality.getElement(this.itemCfg.Quality)
        this.img_bg.imageColor = LinearColor.colorHexToLinearColor(qualityCfg.QualityColor)
    }

    setSelected(select: boolean) {
        this.img_selected.visibility = select ? SlateVisibility.SelfHitTestInvisible : SlateVisibility.Collapsed
    }
}
